#include "companycard.h"
#include "ui_companycard.h"
#include "model/data.h"
CompanyCard::CompanyCard(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::CompanyCard)
{
    ui->setupUi(this);
    init();
}

CompanyCard::~CompanyCard()
{
    delete ui;
}

void CompanyCard::set(const model::CompanyInfo &companyInfo)
{
    if (companyInfo.avatar.isNull())
        ui->label_avatar->setText(companyInfo.companyName);
    else
        ui->label_avatar->setPixmap(companyInfo.avatar.pixmap(ui->label_avatar->size()));
    ui->label_info->setText("近十二个月的面经数：" + QString::number(companyInfo.windowCnt)
                        + "\n最近更新时间：" + util::formatTime(companyInfo.timestamp)
                        + "\n置信度：" + QString::number(companyInfo.ci) + "%");
}

void CompanyCard::init()
{
    initStyleSheet();
    initPanel();
}

void CompanyCard::initStyleSheet()
{
    this->setStyleSheet(R"(background-color:transparent; color: #333333;)");
    ui->panel->setStyleSheet(R"(#panel{border-radius: 12px; background-color: #eeeeee; border: 1px solid #DCDFE4;})"); // DCDFE4
}

void CompanyCard::initPanel()
{
    this->setFixedSize(350, 200);
    this->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Fixed);
    this->layout()->setContentsMargins(10, 0, 0, 0);
    this->layout()->setSpacing(0);

    ui->panel->layout()->setContentsMargins(20, 0, 20, 10);
    ui->panel->layout()->setSpacing(10);

    ui->label_avatar->setFont(QFont("微软雅黑", 20, QFont::Bold));
    ui->label_avatar->setAlignment(Qt::AlignVCenter | Qt::AlignLeft);
    ui->label_avatar->setFixedSize(200, 115);

    ui->label_info->setFont(QFont("微软雅黑", 10));
    ui->label_info->setAlignment(Qt::AlignLeft | Qt::AlignTop);
    ui->label_info->setSizePolicy(QSizePolicy::Expanding, QSizePolicy::Expanding);
}

#include <QMouseEvent>
void CompanyCard::mousePressEvent(QMouseEvent *event)
{
    event->accept();
}

void CompanyCard::mouseReleaseEvent(QMouseEvent *event)
{
    emit clicked();
    event->accept();
}

void CompanyCard::mouseMoveEvent(QMouseEvent *event)
{
    event->accept();
}
